//
//  PopBottomView.m
//  lyx
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 seeday. All rights reserved.
//

#import "OperationView.h"
#import "OperationCell.h"
#import "Sizes.h"
#import "Colors.h"
@implementation OperationView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bounds = [UIScreen mainScreen].bounds;
        self.alpha = 0.0;
        self.backgroundColor = [UIColor blackColor];
        
        self.view_bg = [[UIView alloc]initWithFrame:self.bounds];
        self.view_bg.backgroundColor = [UIColor whiteColor];
        self.view_bg.alpha = 0;
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view_bg.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = self.view_bg.bounds;
        maskLayer.path = maskPath.CGPath;
        self.view_bg.layer.mask = maskLayer;
        
        self.tableView = [[UITableView alloc]initWithFrame:self.view_bg.bounds];
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view_bg addSubview:self.tableView];
        
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelBtn addTarget:self action:@selector(removeOperateView) forControlEvents:UIControlEventTouchUpInside];
        [self.cancelBtn setImage:[UIImage imageNamed:@"icon_equipment6"] forState:UIControlStateNormal];
        [self.cancelBtn setFrame:CGRectMake(ScreenWidth - 55, 0, 50, 50)];
   
        [self.view_bg addSubview:self.cancelBtn];
        
        self.titleLab = [[UILabel alloc] init];
        [self.titleLab setText:@"操作"];
        self.titleLab.font = Font_Regular(18);
        self.titleLab.textAlignment = NSTextAlignmentCenter;
        [self.titleLab setTextColor:rgba(74, 74, 74)];
        [self.view_bg addSubview:self.titleLab];
        self.titleLab.frame = CGRectMake(ScreenWidth/2 - 50, 0, 100, 50);
  
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = rgba(216, 219, 227);
        lineView.frame = CGRectMake(8, 50, ScreenWidth - 16, 0.5);
        [self.view_bg addSubview:lineView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBgAction:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
    }
    return self;
}

-(void)setData:(NSArray *)data{
    if (data.count > 0) {
        
        _data = data;
        CGFloat height_ = _data.count * 55;
        
        CGRect rect_bg = self.view_bg.frame;
        rect_bg.size.height = height_ + 50 + SafeBottomMargin;
        rect_bg.origin.y = ScreenHeight - rect_bg.size.height;
        [self.view_bg setFrame:rect_bg];
        
        
        CGRect rect_tb = self.tableView.frame;
        rect_tb.size.height = height_;
        rect_tb.origin.y = 50;
        [self.tableView setFrame: rect_tb];
        [self.tableView reloadData];
    }
}


#pragma mark----tableView Delegate or dataSource------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentify = @"cellIdentify";
    OperationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (cell == nil) {
        cell = [[OperationCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];
    }

    cell.lab_title.text = _data[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.blockCallBackIndex) {
        self.blockCallBackIndex(indexPath.row);
    }
    [self removeOperateView];
}


-(void)viewShow{
    [[[[UIApplication sharedApplication] windows] firstObject]  addSubview:self];
    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self.view_bg];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.5;
        self.view_bg.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}


-(void)removeOperateView{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        self.view_bg.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.view_bg removeFromSuperview];
    }];
}


#pragma mark-----action-------
-(void)tapBgAction:(UITapGestureRecognizer *)tap{
    
    [self removeOperateView];
}


@end
